package cn.com.dhc.danlu.widget.home;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.dhc.danlu.R;

/**
 * @author: 李一方
 * @time: 2016-08-04 13:53
 * @mail: liyifang@danlu.com
 * @desc: 首页Item加入购物车的按钮
 */
public class JoinShopCarButton extends FrameLayout {
    private TextView tvText;
    private ImageView ivCar;
    private FrameLayout flRoot;

    private float mRawY ;

    public float getRawY() {
        return mRawY;
    }

    public JoinShopCarButton (Context context) {
        super (context);
        init ();
    }

    public JoinShopCarButton (Context context, AttributeSet attrs) {
        super (context, attrs);
        init ();
    }

    private void init () {
        View rootView = LayoutInflater.from (getContext ()).inflate (R.layout.dl_item_home_joincar, this, true);
        ivCar = (ImageView) rootView.findViewById (R.id.iv_car);
        tvText = (TextView) rootView.findViewById (R.id.tv_text);
        flRoot = (FrameLayout) rootView.findViewById (R.id.fl_root);
    }
    public void setOnClickListener(@NonNull OnClickListener onClickListener){
        flRoot.setOnClickListener(onClickListener);
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                break;
            case MotionEvent.ACTION_UP:
                mRawY = event.getRawY();
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * 设置是否缺货按钮的显示状态
     * @param state
     *         是否有货 0 - 有货 : 1 - 暂时缺货
     */
    @TargetApi (Build.VERSION_CODES.JELLY_BEAN)
    public void setMerchState (String state) {
        if (state.equals ("0")) {
            flRoot.setEnabled (true);
            tvText.setVisibility (GONE);
            ivCar.setVisibility (VISIBLE);
        } else {
            flRoot.setEnabled (false);
            tvText.setVisibility (VISIBLE);
            ivCar.setVisibility (GONE);
        }
    }

    public void setText (String text) {
        tvText.setText (text);
    }

    public void setCarImg (int resId) {
        ivCar.setImageResource (resId);
    }
}